import 'package:jpush_flutter/jpush_flutter.dart';

class Push {
  static late JPush jpush;

  static String? registrationID;

  static Future<dynamic> init() {
    jpush = new JPush();
    // 设置
    jpush.setup(
      appKey: '11b1d04fbbac7e3954bb1238',
      channel: 'developer-default',
      production: false,
      // 设置是否打印 debug 日志
      debug: true,
    );
    // 监听
    jpush.addEventHandler(
      // 接收通知回调方法
      onReceiveNotification: (Map<String, dynamic> message) async {
        print("flutter onReceiveNotification: $message");
      },
      // 点击通知回调方法
      onOpenNotification: (Map<String, dynamic> message) async {
        print("flutter onOpenNotification: $message");
      },
      // 接收自定义消息回调方法
      onReceiveMessage: (Map<String, dynamic> message) async {
        print("flutter onReceiveMessage: $message");
      },
    );
    // 申请推送权限
    jpush.applyPushAuthority(new NotificationSettingsIOS(
      sound: true,
      alert: true,
      badge: true,
    ));
    // 获取注册id
    return jpush.getRegistrationID().then((rid) {
      print('获取注册id:' + rid);
      registrationID = rid;
    });
  }
}
